package com.example.demo.controller.user;

import com.example.demo.bean.ErrorCode;
import com.example.demo.bean.JsonData;
import com.example.demo.bean.ThisSystemException;
import com.example.demo.bean.request.UpdateUserInfoRequest;
import com.example.demo.bean.response.LoginResponse;
import com.example.demo.repository.UserRepository;
import com.example.demo.table.UserTable;
import com.example.demo.utli.RedisUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import io.swagger.annotations.Api;

/**
 * 费浩东
 * 2018/11/2
 * Created by fhd
 * @CrossOrigin
 */
@RestController
@Api(description = "个人信息" )
@RequestMapping(value = "/user")
public class PersonalController {

    @Autowired
    UserRepository mUserRepository;

    @Autowired
    RedisUtil mRedisUtil;

    @PostMapping(value = "getUserInfo")
    public JsonData getUserInfo(HttpServletRequest request) {
//        LoginResponse user = (LoginResponse)request.getSession().getAttribute("user");
        String token = request.getHeader("token");

        LoginResponse response = new LoginResponse();
        Object user =  mRedisUtil.get(token);
        BeanUtils.copyProperties(user, response);

//        if (null == user){
//            throw new ThisSystemException(ErrorCode.PLEASE_LOG_IN_FIRST.getText(),ErrorCode.PLEASE_LOG_IN_FIRST.getStatus());
//        }
        Optional<UserTable> users = mUserRepository.findById(response.getUserID());
        if (users.isPresent()){
            UserTable userTable = users.get();
            return JsonData.success(userTable);
        }
        return JsonData.fail(ErrorCode.USER_DOES_NOT_EXIST.getStatus(),ErrorCode.USER_DOES_NOT_EXIST.getText());
    }

    @PostMapping(value = "updateUserInfo")
    public JsonData updateUserInfo(@Valid @RequestBody UpdateUserInfoRequest updateUserInfoRequest , HttpServletRequest request) {
        Optional<UserTable> user = mUserRepository.findById(updateUserInfoRequest.getUserID());
        if (!user.isPresent()) {
            throw new ThisSystemException(ErrorCode.NO_CORRESPONDING_ID_ACCOUNT_WAS_FOUND.getText()
                    ,ErrorCode.NO_CORRESPONDING_ID_ACCOUNT_WAS_FOUND.getStatus());
        }
        UserTable userTable = user.get();
        userTable.setNickName(updateUserInfoRequest.getNickName());
        userTable.setSex(updateUserInfoRequest.getSex());
        userTable.setAvatar(updateUserInfoRequest.getAvatar());
        userTable.setAge(updateUserInfoRequest.getAge());
        mUserRepository.save(userTable);

        mRedisUtil.set(request.getSession().getId(),userTable,10);

        return JsonData.success("已保存");
    }

}
